package services

import (
	"errors"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Services/base"
	"hft/e"
	"hft/global"
	"hft/tools"
)

type CitySetting struct{}

func (*CitySetting) GetManageCities(claims tools.BaseClaims) (list []*admin.Region, err error) {

	var regions []*admin.Region

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return nil, errors.New(e.AccountNotExist)
	}

	err = global.OC_DB.Find(&regions, account.Belong).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	return regions, nil

}

// 获取城市配置
func (*CitySetting) GetCityConfig(claims tools.BaseClaims, id int) (data *admin.CitySetting, err error) {

	var cityConfig *admin.CitySetting

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return nil, errors.New(e.AccountNotExist)
	}

	err = global.OC_DB.Where("region = ?", account.Belong).First(&cityConfig).Error

	if err != nil {
		return nil, errors.New(e.RegionSettingNotFound)
	}

	return cityConfig, nil
}

func (*CitySetting) EditCityConfig(data *admin.CitySetting, claims tools.BaseClaims) (err error) {

	var account *admin.Account

	var region *admin.Region

	var cityConfig *admin.CitySetting

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	res := global.OC_DB.Where("manage_id = ?", account.ID).First(&region)

	if res.Error != nil {
		return errors.New("位置错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该地区信息不存在")
	}

	res = global.OC_DB.Where("region = ?", region.ID).First(&cityConfig)

	if res.Error != nil {
		return errors.New("位置错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("找不到该配置信息")
	}

	global.OC_DB.Model(&admin.CitySetting{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"common_clue_score":  data.CommonClueScore,
		"lou_pan_clue_score": data.LouPanClueScore,
		"per_month_score":    data.PerMonthScore,
		"limit_score":        data.LimitScore,
		"commercial":         data.Commercial,
		"residential":        data.Residential,
		"gong_ji_jin":        data.GongJiJIn,
		"level_a":            data.LevelA,
		"level_b":            data.LevelB,
		"level_c":            data.LevelC,
		"level_d":            data.LevelD,
		"sign_day1":          data.SignDay1,
		"sign_day2":          data.SignDay2,
		"sign_day3":          data.SignDay3,
		"sign_day4":          data.SignDay4,
		"sign_day5":          data.SignDay5,
		"sign_day6":          data.SignDay6,
		"sign_day7":          data.SignDay7,
	})

	return nil
}
func (*CitySetting) CreateCityConfig(ids []int, accountId int) (err error) {

	var addAccount *admin.Account

	err = global.OC_DB.First(&addAccount, accountId).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	for _, v := range ids {
		config := &admin.CitySetting{
			CommonClueScore: 20,
			LouPanClueScore: 10,
			PerMonthScore:   1000,
			LimitScore:      12000,
			Region:          v,
			GongJiJIn:       0.1,
			Commercial:      0.1,
			Residential:     0.1,
			LevelA:          3,
			LevelB:          0.5,
			LevelC:          0,
			LevelD:          0,
			SignDay1:        0,
			SignDay2:        0,
			SignDay3:        0,
			SignDay4:        0,
			SignDay5:        0,
			SignDay6:        0,
			SignDay7:        0,
		}

		global.OC_DB.FirstOrCreate(&config)
	}

	return nil
}

var CitySettingService = new(CitySetting)
